package hch;

public class hch666 {

	public static void main(String[] args) {

		Station station1 = new Station("窗口一");
		Station station2 = new Station("窗口二");
		Station station3 = new Station("窗口三");
		station1.start();
		station2.start();
		station3.start();
	}
}

class Station extends Thread {

	static int ticket = 10;

	static Object ob = "ThisIsATicket";

	public Station(String name) {
		super(name);
	}

	public void run() {
		sell();
	}
	public void sell() {
		while (ticket > 0) {
			synchronized (ob) {
				/**
				 * 使用同步锁控制票
				 */
				if (ticket > 0) {

				if (getName().equals("窗口三") && Math.random() > 0.5 && ticket < 10) {
						ticket++;
						System.out.println("退票第" + ticket + "张票");
				}else {
						System.out.println(getName() + "卖了第" + ticket + "张票");
						ticket--;
					}

			    }else {
					System.out.println("票卖完了");
				}
			}
			/**
			 * 休眠时间随机
			 */
			try {

				sleep((long) (1000 * Math.random()));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
